package com.logicwin.aibala.services.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;

import com.logicwin.aibala.utils.MyLog;

import android.util.Log;

public class FileConnection {
	static final String tag = "FileFunction";
	
	private static String currentPath;
	private static boolean hasSDCard;
	
	public static void init(String path){
//		deleteFold(path);
//		deleteFold("/sdcard/aibala");
		
		MyLog.info("init", "path="+path);
		if (exist("/sdcard")){
			currentPath = "/sdcard/aibala";
			if (!exist(currentPath)){
				createFold(currentPath);
			}
		}
		else {
			currentPath = path;
		}
//		currentPath = path;
	}
	
	public static String getCurrentPath(){
		return currentPath;
	}
	
	public static String readFile(String fileName) {
		FileInputStream fis = null;
		String str = "";
		try {
			File file = new File(fileName);
			fis = new FileInputStream(file);
			byte[] buff = new byte[fis.available()];
			fis.read(buff);
			str = new String(buff);
		} catch (FileNotFoundException e) {
			Log.w(tag, "readFile::" + e.getMessage());
		} catch (IOException e) {
			Log.w(tag, "readFile::" + e.getMessage());
		} finally {
			if (null != fis) {
				try {
					fis.close();
				} catch (IOException e) {
					Log.w(tag, "readFile::" + e.getMessage());
				}
			}
		}
		return str.trim();
	}

	public static InputStream getFileStream(String fileName) {
		InputStream is = null;
		try {
			File file = new File(fileName);
			is = new FileInputStream(file);
		} catch (FileNotFoundException e) {
			Log.w(tag, "getFileStream::" + e.getMessage());
		}
		return is;
	}

	public static void writeFile(String fileName, String str) {

		FileOutputStream fout = null;
		OutputStreamWriter osw = null;
		File file = new File(fileName);
		try {
			fout = new FileOutputStream(file, false);
			osw = new OutputStreamWriter(fout);
			osw.write(str);
			osw.flush();
		} catch (Exception e) {
			Log.w(tag, "writeFile::" + e.getMessage());
		} finally {
			try {
				osw.close();
				fout.close();
			} catch (IOException e) {
				Log.w(tag, "writeFile::" + e.getMessage());
			}
		}
	}

	public static boolean exist(String fileName) {
		File file = new File(fileName);
		return file.exists();
	}

	public static void deleteFile(String fileName) {
		File file = new File(fileName);
		if (file.exists() && file.isFile()) {
			file.delete();
		}
	}

	public static void createFold(String path) {
		File file = new File(path);
		if (!file.exists()) {
			file.mkdirs();
		}
	}

	public static void deleteFold(String path) {
		File file = new File(path);
		if (file.exists() && file.isDirectory()) {
			if (!file.delete()) {
				File[] fs = file.listFiles();
				for (int i = 0; i < fs.length; i++) {
					if (fs[i].isDirectory()) {
						deleteFold(fs[i].getAbsolutePath());
					} else {
						deleteFile(fs[i].getAbsolutePath());
					}
				}
				file.delete();
			}
		}
	}

	public static void chmodFile(String filePath) {
		try {
			String command = "chmod 777 " + filePath;
			Runtime runtime = Runtime.getRuntime();

			runtime.exec(command);
		} catch (IOException e) {
			Log.w(tag, "ChmodFile::" + e.getMessage());
		}
	}

	public static boolean isFold(String filePath) {
		File file = new File(filePath);
		return file.isDirectory();
	}

	public static boolean isFile(String filePath) {
		File file = new File(filePath);
		return file.isFile();
	}

	/**
	 * 复制单个文件
	 * 
	 * @param oldPath
	 *            String 原文件路径 如：c:/fqf.txt
	 * @param newPath
	 *            String 复制后路径 如：f:/fqf.txt
	 * @return boolean
	 */
	public static void copyFile(String oldPath, String newPath) {
		try {
//			int bytesum = 0;
			int byteread = 0;
			File oldfile = new File(oldPath);
			if (oldfile.exists()) { // 文件存在时
				InputStream inStream = new FileInputStream(oldPath); // 读入原文件
				FileOutputStream fs = new FileOutputStream(newPath);
				byte[] buffer = new byte[1444];
				//int length;
				while ((byteread = inStream.read(buffer)) != -1) {
//					bytesum += byteread; // 字节数 文件大小
					//System.out.println(bytesum);
					fs.write(buffer, 0, byteread);
				}
				inStream.close();
			}
		} catch (Exception e) {
		}
	}
	
	public static int getFileSize(String filePath){
		File file = new File(filePath);
		try{
			FileInputStream fis = new FileInputStream(file);  
            return fis.available();
        }catch(IOException e1){   
        }
        return 0;
	}

}
